package com.yes.yll.cuize;

import com.yes.yll.cuize.Goods;
import com.yes.yll.cuize.Human;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) throws Exception {
    /*List<Integer> ns=new ArrayList<>();
    ns.add(12);
    ns.add(23);
        System.out.println(ns);*/



       /* Class<Animal> a = Animal.class;
        Object b = a.newInstance();
        Field[] fields = a.getDeclaredFields();

        for(Field field:fields){
            field.setAccessible(true);
            Class<?> type = field.getType();

            if(type.isAssignableFrom(String.class)){

                field.set(b,RandomStringUtils.randomAlphabetic(6));

            }else if(type.isAssignableFrom(Double.class)) {

                field.set(b,RandomUtils.nextDouble());
            }
        }
        System.out.println(b);
*/
        /*List<Integer> ns = gens(3, Integer.class);
        List<String> ss = gens(6, String.class);
        System.out.println(ns);
        System.out.println(ss);*/

        //Integer n = gen(Integer.class);
        //String s = gen(String.class);


        Human u = genObj(Human.class);
        Goods g = genObj(Goods.class);
        System.out.println(u);
        System.out.println(g);
        Integer n = genObj(Integer.class);
        String s = genObj(String.class);
        System.out.println(n);
        System.out.println(s);


    }
    public static <Y> Y gen(Class<Y> a){
        Y y = null;
        if(a.isAssignableFrom(Integer.class)){
            Integer i = RandomUtils.nextInt();
            y = (Y)i;
        }else if(a.isAssignableFrom(String.class)){
            String s = RandomStringUtils.randomAlphabetic(5);
            y=(Y)s;
        }
        return y;
    }
    public static <Y> List<Y> gens(Integer num,Class<Y> c){
        List<Y> ys=new ArrayList<>();
        for (int i = 0; i <num ; i++) {
            Y y = gen(c);
            ys.add(y);
        }
        return ys;
    }

    public static <Y> Y genObj(Class<Y> b) throws Exception {
        Y y=null;

        if (b.isAssignableFrom(Integer.class)){
            Integer i = RandomUtils.nextInt();
            y=(Y)i;
        }else  if (b.isAssignableFrom(String.class)){
            String s = RandomStringUtils.randomAlphabetic(5);
            y=(Y)s;
        }else {
            y=b.newInstance();

            Field[] fields = b.getDeclaredFields();

            for(Field field:fields){
                Class<?> type = field.getType();
                field.setAccessible(true);
                if(type.isAssignableFrom(Integer.class)){
                    int i = RandomUtils.nextInt();
                    field.set(y,i);
                }else if (type.isAssignableFrom(String.class)){
                    String s = RandomStringUtils.randomAlphabetic(5);
                    field.set(y,s);
                }else if(type.isAssignableFrom(Double.class)){
                    double v = RandomUtils.nextDouble();
                    field.set(y,v);
                }

            }

        }
       return y;
    }

  /*  public static <Y> List<Y> genObj(Integer num,Class<Y> b) throws Exception{
        Y y=null;
        //if(b.isAssignableFrom())


    }*/
}
